package com.rd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rd.mapper.DepartmentMapper;
import com.rd.pojo.Department;
import com.rd.service.IDepartmentService;
import com.rd.util.ResultModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiufenfen
 * @since 2023-10-28
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Resource
    DepartmentMapper departmentMapper;

    @Override
    public ResultModel getAllDepartments() {
        List<Department> list= departmentMapper.getAllDepartments(-1);
        return ResultModel.createBySuccess().put("deptList",list);
    }

    @Override
    public ResultModel addDept(Department dept) {
        dept.setEnabled(true);
        departmentMapper.addDept(dept);
        if ( dept.getResult() == 1) {
            //把添加成功的部门返回给前端页面
            return ResultModel.createBySuccess("添加成功！").put("dept",dept);
        }
        return ResultModel.createByErrorCodeMessage("添加失败！");
    }

    @Override
    public ResultModel deleteDept(Integer id) {
        Department dep = new Department();
        dep.setId(id);
        departmentMapper.deleteDept(dep);
        if (-2 == dep.getResult()) {
            return ResultModel.createByErrorCodeMessage("该部门下还有子部门，删除失败！");
        }
        if (-1 == dep.getResult()) {
            return ResultModel.createByErrorCodeMessage("该部门下还有员工，删除失败！");
        }
        if (1 == dep.getResult()) {
            return ResultModel.createBySuccess("删除成功！");
        }
        return ResultModel.createByErrorCodeMessage("删除失败！");
    }
}
